package com.tj.jinzao.manager;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.tj.jinzao.App;
import com.tj.jinzao.common.Settings;
import com.tj.jinzao.model.User;
import com.tj.jinzao.provider.ProviderContract;

/**
 * 用户管理
 * Created by wjy on 2018/4/29.
 */

public class UserManager {

    private static User sUser;
    private static int sVersion = -1;

    private static class LazyHolder {
        private static final UserManager INSTANCE = new UserManager();
    }

    private UserManager() {
    }

    public static final UserManager getInstance() {
        return LazyHolder.INSTANCE;
    }

    public static void saveUserData(User user) {
        Log.w("用户信息","存储"+user.toString());
        if(user.getOpenid()!=null&&!user.getOpenid().equals("")&&!user.getOpenid().equals("null")){
            user.setUnionid(user.getOpenid());
        }
        Settings.setLong(App.getInstance(), Settings.KEY_PREF_USER_ID, user.getId());
        if (user == null) return;
        sUser = user;
        ContentValues value = new ContentValues();
        value.put(ProviderContract.User.USER_ID, user.getId());
        value.put(ProviderContract.User.DATA, new Gson().toJson(user));
        value.put(ProviderContract.UPDATE_ON_INSERT_FAILURE, true);
        value.put(ProviderContract.UPDATE_ON_INSERT_FAILURE_KEY, ProviderContract.User.USER_ID);
        try {
            App.getInstance().getContentResolver().insert(ProviderContract.User.CONTENT_URI, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public User getUser() {
        if (sUser != null) return sUser;
        else return getUserInfoFromDb();
    }

    public User getUserInfoFromDb() {
        sUser = getUserInfoFromDb(App.getInstance());
        return sUser;
    }

    public User getUserInfoFromDb(Context context) {
        if (context == null) return null;
        User user = new User();
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(ProviderContract.User.CONTENT_URI, null
                    , null, null, null);
            if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
                String data = cursor.getString(cursor.getColumnIndex(ProviderContract.User.DATA));
                if (!TextUtils.isEmpty(data)) {
                    user = new Gson().fromJson(data, User.class);
                    return user;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) cursor.close();
        }
        return user;
    }

    public static void userLogout() {
        sUser = null;
        Settings.setInt(App.getInstance(), Settings.KEY_PREF_USER_TOKEN, 0);
        Settings.setLong(App.getInstance(), Settings.KEY_PREF_USER_ID, 0);
        App.getInstance().getContentResolver().delete(ProviderContract.User.CONTENT_URI
                , null, null);
    }
}
